﻿/*
    Project: IV:MP GUI Editor (http://code.google.com/p/ivmp-gui-editor)
    Code license: GNU Lesser GPL (http://www.gnu.org/copyleft/lesser.html) 
 */

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace IvmpEditor
{
    public class GUIWindow : GUIContainer
    {
        public new const string TYPE = "GUIWindow";
        public override string GetTypeName() { return TYPE; }
        public override string GetDefaultVarName() { return "window"; }
        public new Window NativeControl { get { return nativeControl as Window; } }
        public Panel Workbench = new Canvas();

        public GUIWindow(Window nativeWindow, string _variableName)
        {
            nativeControl = nativeWindow;
            variableName = _variableName;
            containerElements = new List<GUIElement>(40);
            properties = new Dictionary<string, object>(30);
        }
        protected override void MakeControl()
        {
            throw new NotSupportedException();
        }

        // GUIElementContainer:
        public override void ChildAdd(GUIElement child)
        {
            if (ChildExists(child.GetName()))
                throw new ArgumentException("This element is already added to this window.");
            else if (child.GetParent() != this)
                throw new ArgumentException("Incorrect usage: child parent must be set to this GUIWindow instance already.");

            containerElements.Add(child);
            Workbench.Children.Add(child.NativeControl);
        }
        public override void ChildRemove(GUIElement child)
        {
            if (child == null || !ChildExists(child.GetName()))
                throw new ArgumentNullException("child");
            else if (child.GetParent() != null)
                throw new ArgumentException("Incorrect usage: child parent must be set to null.");
            containerElements.Remove(child);
            Workbench.Children.Remove(child.NativeControl);
        }
        
        // Overrides:
        public override void SetText(string newTxt, bool native)
        {
            base.SetText(newTxt, native);
            // We do not change any native property here.
        }
        public override void SyncFromNative()
        {
            base.SyncFromNative();

            // base function syncs a size of WINDOW, but we need workbench panel's size:            
            PropertySet("Size",
                new Point(
                    Workbench.ActualWidth + 2.3,
                    Workbench.ActualHeight + 17
                ));

            // Syncing children elems
            foreach (var elem in containerElements)
                elem.SyncFromNative();
        }
        public override void SyncToNative()
        {
            base.SyncToNative();
            if (NativeControl != null)
                SetText(PropertyGet("Text") as string, true);
        }
        public override string GetSQCode(ref SquirrelCodeStack sq)
        {
            SyncFromNative();
            sq.wComment("Code of " + variableName + " GUI window BEGINS");

            // Window self code:
            sq.wSetTable(variableName, true);
            window_self_code(ref sq);

            // Children code:
            foreach (var elem in containerElements)
            {
                string elemVar = elem.GetSQCode(ref sq);
                sq.wFunc(null, elemVar, "setParent", variableName + ".window.getName()");
            }
            sq.wComment("Code of " + variableName + " GUI window ENDS");

            return variableName;
        }
        private void window_self_code(ref SquirrelCodeStack sq)
        {
            sq.wSet(null, "window", TYPE + "()", true);
            WriteElementCommonCode(ref sq, new Point(), "window");
        }
        public SquirrelCodeStack GetSQCode()
        {
            var sq = new SquirrelCodeStack();
            GetSQCode(ref sq);
            return sq;
        }
        public override Style GetBaseStyle()
        {
            return App.Current.FindResource("CEGUI_Window") as Style;
        }
    }
}